######################################################################################################################################################
### Setup 

# Source setup file, define which congresses
setwd('~/Dropbox/Projects/Dissertation/Bill Text and Agenda Control/JOP Replication Files')
source('text_setup.R')
congs <- 104:114

# Load data
load('Data/BTAC_predNonFigures.RData')



###############################################################################################################################################################################
### Table 3

# Floor bills
floor <- function(df){
  with(df,
       data.frame(
         successes=sum(successes),
         disses=sum(disses),
         rolls=sum(rolls),
         blocks=sum(blocks),
         total=sum(successes, disses, rolls, blocks)
       ) 
  ) %>%
    mutate(succProp=successes/total) %>%
    mutate(dissProp=disses/total) %>%
    mutate(rollProp=rolls/total) %>%
    mutate(blockProp=blocks/total)
}
houseFloor <- data.frame(chamber='house', floor(houseqoisV))
senFloor <- data.frame(chamber='senate', floor(senqoisV))
floorAC <- rbind(houseFloor, senFloor)


# Pre-floor bills
prefloor <- function(df, chamber){
  if(chamber=='house'){
    uncert <- filter(suppHDist, cong=='total')
  } else if(chamber=='senate'){
    uncert <- filter(suppSDist, cong=='total') 
  }
  res <- with(df,
              data.frame(
                misses=sum(misses),
                missHi=sum(misses+round(misses*uncert$missHi)),
                missLo=sum(misses-round(misses*uncert$missLo)),
                disses=sum(disses),
                dissHi=sum(disses+round(disses*uncert$dissHi)),
                dissLo=sum(disses-round(disses*uncert$dissLo)),
                dodges=sum(dodges),
                dodgeHi=sum(dodges+round(disses*uncert$dodgeHi)),
                dodgeLo=sum(dodges-round(disses*uncert$dodgeLo)),
                blocks=sum(blocks),
                blockHi=sum(blocks+round(blocks*uncert$blockHi)),
                blockLo=sum(blocks-round(blocks*uncert$blockLo)),
                total=sum(misses, disses, dodges, blocks)
              ) 
  ) %>%
    #mutate(test=misses+round(misses*uncert$missHi))
    mutate(missProp=misses/total) %>%
    mutate(dissProp=disses/total) %>%
    mutate(dodgeProp=dodges/total) %>%
    mutate(blockProp=blocks/total) 
  res[res < 0] <- 0
  res <- cbind(chamber, res)
  res
}
housePre <- prefloor(houseqois, chamber='house')
senPre <- prefloor(senqois, chamber='senate')
preAC <- rbind(housePre, senPre)

# All info for AC table
floorAC
preAC



###############################################################################################################################################################################
### Figure 4

### Calculate vectors (and add uncertainty)

# New suppDist objects for plotting (remove total column)
suppHDistPlot <- filter(suppHDist, cong!='total')
suppSDistPlot <- filter(suppSDist, cong!='total')

# P(floor | support) -- House
vsH <- with(houseqoisV, successes+disses)/(houseqois$support + houseqoisV$support)
vsH.up <- vsH + (abs(suppHDistPlot$hiU)*(houseqois$n/(houseqois$n+houseqoisV$n)))
vsH.down <- vsH - (abs(suppHDistPlot$loU)*(houseqois$n/(houseqois$n+houseqoisV$n)))

# P(floor | support) -- Senate
vsS <- with(senqoisV, successes+disses)/(senqois$support + senqoisV$support)
vsS.up <- vsS + (abs(suppSDistPlot$hiU)*(senqois$n/(senqois$n+senqoisV$n)))
vsS.down <- vsS - (abs(suppSDistPlot$loU)*(senqois$n/(senqois$n+senqoisV$n)))

# P(pre-floor | oppose) -- House
voH <- (with(houseqois, dodges+blocks)/(with(houseqois, n-support)+with(houseqoisV, n-support)))
voH.up <- voH + (abs(suppHDistPlot$hiU)*(houseqois$n/(houseqois$n+houseqoisV$n)))
voH.down <- voH - (abs(suppHDistPlot$loU)*(houseqois$n/(houseqois$n+houseqoisV$n)))

# P(pre-floor | oppose) -- Senate
voS <- (with(senqois, dodges+blocks)/(with(senqois, n-support)+with(senqoisV, n-support)))
voS.up <- voS + (abs(suppSDistPlot$hiU)*(senqois$n/(senqois$n+senqoisV$n)))
voS.down <- voS - (abs(suppSDistPlot$loU)*(senqois$n/(senqois$n+senqoisV$n)))
voS.up[voS.up > 1] <- 1


### Plot 
tiff(filename='Figures/fg4.tif', width=10, height=5, units='in', res=1200)
par(mfrow=c(1,2), las=2)
plot(congs, houseqoisV$supportProp, type='n', ylim=c(0,1), main='Floor Bills',
     ylab='Proportion Opposed by Majority Party', xlab='Congress', xaxt='n')
grid()
lines(congs, 1-houseqoisV$supportProp, type='b', lty=1, pch=15, lwd=1)
lines(congs, 1-senqoisV$supportProp, type='b', lty=2, pch=17, lwd=1)
legend(x='topleft', bty='n', lty=c(1,2), pch=c(15, 17), 
       legend=c('House', 'Senate'))
axis(side=1, at=congs)

plot(congs, voH, type='n', xlab='Congress', ylab='Proportion Failing to Reach Floor', 
     ylim=c(0,1), main='Bills Opposed by Majority Party', xaxt='n')
grid()
lines(congs, voH, type='b', pch=15, lty=1)
arrows(x0=congs, x1=congs, y0=voH.down, y1=voH.up, code=3, angle=90, length=0.05, lwd=2)
lines(congs, voS, type='b', pch=17, lty=2)
arrows(x0=congs, x1=congs, y0=voS.down, y1=voS.up, code=3, angle=90, length=0.05, lwd=2)
legend(x='bottomleft', bty='n', legend=c('House', 'Senate'), 
       pch=c(15, 17), lty=c(1,2))
axis(side=1, at=congs)

par(mfrow=c(1,1), las=1)
dev.off()



###############################################################################################################################################################################
### Figure 5

### Plot
tiff(filename='Figures/fg5.tif', width=10, height=5, units='in', res=1200)
par(mfrow=c(1,2), las=2)

plot(congs, houseqois$supportProp, type='n', ylim=c(0,1), main='Pre-Floor Bills',
     ylab='Proportion Supported by Majority Party', xlab='Congress', xaxt='n')
grid()
lines(congs, houseqois$supportProp, type='b', lty=1, pch=15, lwd=1)
lines(congs, senqois$supportProp, type='b', lty=2, pch=17, lwd=1)
arrows(x0=congs, x1=congs, y0=houseqois$supportProp-suppHDistPlot$loU, 
       y1=houseqois$supportProp+suppHDistPlot$hiU, code=3, angle=90, length=0.05, lwd=2)
arrows(x0=congs, x1=congs, y0=senqois$supportProp-suppSDistPlot$loU, 
       y1=senqois$supportProp+suppSDistPlot$hiU, code=3, angle=90, length=0.05, lwd=2)
legend(x='topleft', bty='n', lty=c(1,2), pch=c(15, 17), 
       legend=c('House', 'Senate'))
axis(side=1, at=congs)

plot(congs, vsH, type='n', xlab='Congress', ylab='Proportion Reaching Floor', 
     ylim=c(0,1), main='Bills Supported by Majority Party', xaxt='n')
grid()
lines(congs, vsH, type='b', pch=15, lty=1)
arrows(x0=congs, x1=congs, y0=vsH.down, y1=vsH.up, code=3, angle=90, length=0.05, lwd=2)
lines(congs, vsS, type='b', pch=17, lty=2)
arrows(x0=congs, x1=congs, y0=vsS.down, y1=vsS.up, code=3, angle=90, length=0.05, lwd=2)
legend(x='bottomleft', bty='n', legend=c('House', 'Senate'),
       pch=c(15, 17), lty=c(1,2))
axis(side=1, at=congs)

par(mfrow=c(1,1), las=1)
dev.off()
